using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml;


namespace OfficialGameProject
{
    class ParticleSystem
    {
        private Random random;
        private SpriteBatch spriteBatch;
        private List<Particles> listParticles;
        private Dictionary<string, Texture2D> textureDictionary;
        private ContentManager contentManager;

        public ParticleSystem()
        {
            random=new Random();
            ResetParticles();
        }

        public void ResetParticles()
        {
            listParticles= new List<Particles>();
        }

        public ParticleSystem(ContentManager contentManager, SpriteBatch spriteBatch)
        {
            random=new Random();
            ResetParticles();
            InitializeAssets(contentManager,spriteBatch);
        }


        public void InitializeAssets(ContentManager contentManager, SpriteBatch spriteBatch)
        {
            this.contentManager = contentManager;
            this.spriteBatch = spriteBatch;
            textureDictionary=new Dictionary<string, Texture2D>();
            textureDictionary["smoke"] = contentManager.Load<Texture2D>("smoke");
            textureDictionary["fire"] = contentManager.Load<Texture2D>("fire");
        }

        public void Setparticles()
        {
            foreach (Particles particle in listParticles)
            {
                particle.texture = textureDictionary[particle.textureName];
            }
        }

        public void Update(float elaped)
        {
            
            for (int i = 0; i < listParticles.Count; ++i)
            {
                Particles particle = listParticles[i];
                particle.life -= elaped;
                particle.particlesPosition += particle.velocity*elaped;
                particle.rotation += particle.rotation*elaped;
                particle.alpha += particle.alphaRate*elaped;
                particle.scale += particle.scaleRate*elaped;

                if (particle.alpha<0.0f)
                {
                    particle.alpha = 0.0f;
                }
            }
        }

        public void Draw()
        {
            for (int i = 0; i < listParticles.Count; ++i)
            {
                Particles particles = listParticles[i];

                float alphaF = 255.0f * particles.alpha;
                if (alphaF < 0.0f)
                    alphaF = 0.0f;
                if (alphaF > 255.0f)
                    alphaF = 255.0f;

                spriteBatch.Draw(particles.texture, particles.particlesPosition, null,
                                 new Color(particles.color.R, particles.color.G, particles.color.B, (byte) alphaF),
                                 particles.rotation,
                                 new Vector2(particles.texture.Width, particles.texture.Height), particles.scale,
                                 SpriteEffects.None, 0);
            }
        }

        Particles CreateParticle()
        {
            Particles p = null;

            for (int i = 0; i < listParticles.Count; ++i)
            {
                if (listParticles[i].life <= 0.0f)
                {
                    p = listParticles[i];
                    break;
                }
            }

            if (p == null)
            {
                p = new Particles();
                listParticles.Add(p);
            }

            p.color = Color.White;

            return p;
        }

        public void CreateEffect(Vector2 position)
        {
            Particles p = null;

            for (int i = 0; i < 1; ++i)
            {
                p = CreateParticle();
                p.particlesPosition = position;
                p.rotationRate = -2.0f + 4.0f * (float)random.NextDouble();
                p.scale = 0.25f;
                p.scaleRate = 1.0f *(float)random.NextDouble();
                p.alpha = 2.0f;
                p.alphaRate = -10.0f;
                p.velocity = Vector2.Zero;
                p.textureName = "fire";
                p.texture = textureDictionary[p.textureName];
                p.life = 0.4f;
            }
        }

        class Particles
        {
            public Vector2 particlesPosition;
            public Vector2 velocity;

            public Texture2D texture;

            public string textureName;
            public float rotationRate;
            public float rotation;
            public float life;
            public float alphaRate;
            public float alpha;
            public float scaleRate;
            public float scale;
            public Color color = new Color();
        }
    }
}
